<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName" required>
              <el-input v-model="form.warehouseName" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库编号" prop="warehouseCode" required>
              <el-input v-model="form.warehouseCode" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="仓库地址" prop="warehouseLocation" required>
              <el-input v-model="form.warehouseLocation" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="userId" required>
              <el-select v-model="form.userId" filterable style="width: 100%">
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.nickName+'（'+item.deptName+'）'"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="是否默认" prop="isDefault" required>
              <el-select v-model="form.isDefault" style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_yes_no"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <titleDiv name="仓位信息"></titleDiv>
      <div v-if="flag!=1">
        <el-button type="primary" plain @click="list.push({isDefaultSpace:'0'})">新增</el-button>
        <el-button @click="list=[]">清空</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="list"
        max-height="500"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="spaceName" label="仓位名称" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.spaceName }}</div>
            <el-input v-else v-model="scope.row.spaceName" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="spaceCode" label="仓位编码" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.spaceCode }}</div>
            <el-input v-else v-model="scope.row.spaceCode" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="负责人" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.userName }}</div>
            <el-select v-else v-model="scope.row.userId" filterable style="width: 100%">
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.nickName+'（'+item.deptName+'）'"
                :value="item.userId">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="spaceLocation" label="仓位地址" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.spaceLocation }}</div>
            <el-input v-else v-model="scope.row.spaceLocation" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="isDefaultSpace" label="是否默认" align="center">
          <template slot-scope="scope">
            <dict-tag v-if="flag==1" :options="dict.type.sys_yes_no" :value="scope.row.isDefaultSpace"/>
            <el-select v-else v-model="scope.row.isDefaultSpace" filterable style="width: 100%">
              <el-option
                v-for="item in dict.type.sys_yes_no"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.remark }}</div>
            <el-input v-else v-model="scope.row.remark" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" align="center" width="80" v-if="flag!=1">
          <template slot-scope="scope">
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addStore, updateStore} from "../../../api/imsMng";
import {listUser} from "@/api/system/user";

export default {
  dicts: ['sys_yes_no'],
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {},
      list: [],
      userList: [],
      titleList: ['新增', '详情', '修改']
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.list = []
      this.form = form
      if (flag) {
        this.list = form.warehouseSpaceVoList
      }
      listUser({pageSize: -1}).then(res => {
        this.userList = res.rows
      })
    },
    deleteRow(row) {
      this.list = this.list.filter(val => val != row)
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.warehouseSpaceBoList = this.list
          if (this.flag == 2) {
            updateStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
  }
}
</script>
